from django.db import models
from django.utils import timezone

class Banner(models.Model):
    imgurl = models.ImageField(u'轮播图图片地址', upload_to='banners')
    content_url = models.URLField(u'轮播图链接地址', blank=True, default='')
    weight = models.SmallIntegerField(u'优先级', default=1)  # 越小越靠前
    add_time = models.DateTimeField(u'添加时间', default=timezone.now, blank=True)

    def __str__(self):
        return self.imgurl

    class Meta:
        verbose_name = '轮播图'
        verbose_name_plural = '轮播图'


class Category(models.Model):
    name = models.CharField(u'类别名', max_length=50)
    level = models.CharField(u'分类级别', max_length=2)  # 1表示一级分类，2表示二级分类
    top_level = models.ForeignKey('self', on_delete=models.SET_DEFAULT, default=0, blank=True, verbose_name=u'上级分类')  # 一级分类此值为空或0
    index_show = models.CharField(u'是否在首页显示', max_length=1, default='0')   # 0表示不显示,1表示显示

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '图书类别'
        verbose_name_plural = '图书类别'


class Book(models.Model):
    name = models.CharField(u'书名',max_length=100, default='')
    author = models.CharField(u'作者', max_length=50, blank=True, default='')
    cover = models.ImageField(u'封面图', upload_to='covers')
    publish_time = models.CharField(u'出版时间', max_length=20, blank=True, default='未知')
    publish_house = models.CharField(u'出版社', max_length=100, blank=True, default='')
    page_count = models.SmallIntegerField(u'页数', blank=True, default=0)
    collect_count = models.SmallIntegerField(u'藏书量', blank=True, default=0)
    remain_count = models.SmallIntegerField(u'剩余量', blank=True, default=0)
    ISBN = models.CharField(u'ISBN', max_length=30, blank=True, default='')
    category = models.ForeignKey(Category, on_delete=models.SET_DEFAULT, blank=True, default=1, verbose_name=u'类别')
    content_introduction = models.TextField(u'内容简介', blank=True, default='')
    author_introduction = models.TextField(u'作者简介', blank=True, default='')
    directory = models.TextField(u'目录', blank=True, default='')
    add_time = models.DateTimeField(u'添加时间', blank=True, default=timezone.now)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '图书'
        verbose_name_plural = '图书'